package com.zonski.jbots.game.resources;

import com.zonski.jbots.engine.*;
import com.zonski.jbots.game.JBotsConstants;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 14, 2003
 * Time: 8:51:48 AM
 * To change this template use Options | File Templates.
 */
public class LaserGun extends IntermittentResource
{
    private static final int DEFAULT_COST = 1;

    private String toCreate;
    private Entity laser;

    public LaserGun(int updatesBetweenCreations, String toCreate)
    {
        super(updatesBetweenCreations);
        this.toCreate = toCreate;
    }

    public void stopUsing()
    {
        super.stopUsing();

        // kill the laser
        if(this.laser != null)
        {
            laser.setRemovable(true);
            laser = null;
            this.getOwner().getDevice().play(JBotsConstants.LASER, Device.STOP);
        }
    }

    public void startUsing(int direction)
    {
        super.startUsing(direction);
        this.getOwner().getDevice().play(JBotsConstants.LASER, Device.LOOP);
    }

    public int getCurrentChargeCost()
    {
        return DEFAULT_COST;
    }

    protected Entity create(Engine engine)
        throws Exception
    {
        Entity entity;
        if(this.laser == null)
        {
            entity = engine.getEntityFactory().createEntity(this.toCreate);
            this.laser = entity;

            int direction = this.getOwner().direction;
            // set the position
            int x;
            int y;
            Rectangle ownerBounds = this.getOwner().bounds;
            Rectangle bounds = entity.bounds;
            switch(direction)
            {
                case Directions.EAST:
                case Directions.NORTH_EAST:
                case Directions.SOUTH_EAST:
                    x = ownerBounds.rightEdge;
                    break;
                case Directions.WEST:
                case Directions.NORTH_WEST:
                case Directions.SOUTH_WEST:
                    x = ownerBounds.x - bounds.width;
                    break;
                default:
                    x = ownerBounds.getCentreX() - bounds.width/2;
                    break;
            }

            switch(direction)
            {
                case Directions.SOUTH:
                case Directions.SOUTH_WEST:
                case Directions.SOUTH_EAST:
                    y = ownerBounds.y - bounds.height;
                    break;
                case Directions.NORTH:
                case Directions.NORTH_WEST:
                case Directions.NORTH_EAST:
                    y = ownerBounds.bottomEdge;
                    break;
                default:
                    y = ownerBounds.getCentreY() - bounds.height/2;
                    break;
            }
            entity.bounds.setLocation(x, y);
            entity.direction = direction;
            entity.layerId = this.getOwner().layerId;
        }else{
            entity = null;
        }
        return entity;
    }

    public Resource copy()
    {
        LaserGun gun = new LaserGun(this.getUpdatesBetweenCreations(), this.toCreate);
        gun.setKillOwner(this.getKillOwner());
        return gun;
    }
}
